package code;

import java.util.ArrayList;
import java.util.List;

public class GetIntersectionNode_day07 {
    public static void main(String[] args) {
        ListNode head = new ListNode(0);head.next = null;
        ListNode temp1= new ListNode(1);head.next = temp1;
        ListNode temp2= new ListNode(2);temp1.next = temp2;
        ListNode temp3= new ListNode(3);temp2.next = temp3;
        ListNode temp4= new ListNode(4);temp3.next = temp4;
        ListNode temp5= new ListNode(5);temp4.next = temp5;
        ListNode head2= new ListNode(0);head2.next =null;
        ListNode temp6= new ListNode(6);head2.next = temp6;
        ListNode temp7= new ListNode(7);temp6.next = temp7;
        ListNode temp8= new ListNode(8);temp7.next = temp8;
        temp8.next = temp3;
        //0-1-2-3-4-5
        //0-6-7-8-3-4-5
//        System.out.println(temp8.next.equals(temp2.next));
        ListNode result= getIntersectionNode(head, head2);
        System.out.println(result.val);
    }
    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        List<ListNode> nodes = new ArrayList<>();
        ListNode curr1 = headA;
        while (curr1 != null) {
            nodes.add(curr1);
            curr1 = curr1.next;
        }
        ListNode curr2= headB;
        while (curr2 != null) {
            if(nodes.contains(curr2)) {
                return curr2;
            }
            curr2= curr2.next;
        }
        return null;
    }
    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
